/*
 * Copyright (c) 2024-2025 LTC-CN. All rights reserved.
 * <p>
 * This file is part of the ltc-cn-springboot project.
 * <p>
 * ltc-cn-springboot is distributed under the terms of the MIT License.
 * For details, see the accompanying LICENSE file or visit:
 * <a href="https://www.ltc-cn.top/projects">...</a>
 */

package top.ltc_cn.minecraft_manager.config;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.config.SaCookieConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.ltc_cn.minecraft_manager.properties.SaCookieCurProperties;

@Slf4j
@Configuration
public class SaCookieConfiguration {

    @Autowired
    private SaCookieCurProperties saCookieCurProperties;

    private void setSaCookieConfig(SaCookieConfig saCookieConfig) {
        saCookieConfig.setSameSite(saCookieCurProperties.getSameSite() == null ? "none" : saCookieConfig.getSameSite());
        saCookieConfig.setDomain(saCookieCurProperties.getDomain() == null ? "localhost" : saCookieConfig.getDomain());
        saCookieConfig.setPath(saCookieCurProperties.getPath() == null ? "/" : saCookieConfig.getPath());
        saCookieConfig.setSecure(saCookieCurProperties.isSecure());
        saCookieConfig.setHttpOnly(saCookieCurProperties.isHttpOnly());

        // 设置cookie
        SaManager.getConfig().setCookie(saCookieConfig);
        saCookieConfig = SaManager.getConfig().getCookie();

        // 增加日志记录
        log.info("[自定义Cookie设置] SaCookieConfig configured with sameSite: {}, domain: {}, path: {}, secure: {}, httpOnly: {}",
                saCookieConfig.getSameSite(), saCookieConfig.getDomain(), saCookieConfig.getPath(),
                saCookieConfig.getSecure(), saCookieConfig.getHttpOnly());

    }

    @Bean
    public CommandLineRunner saCookieConfigurer() {
        return args -> {
            try {
                setSaCookieConfig(SaManager.getConfig().getCookie());
            } catch (Exception e) {
                log.error("Failed to configure SaCookieConfig", e);
                throw new RuntimeException("Failed to configure SaCookieConfig", e);
            }
        };
    }

//    @PostConstruct
//    public void saTokenPostConstruct() {
//        // 指定防火墙校验不通过时的处理方案
//        SaFirewallStrategy.instance.requestPathInvalidHandle = (e, req, res, extArg) -> {
//            System.out.println("防火墙校验不通过：" + e.getMessage());
//            try {
//                HttpServletResponse response = (HttpServletResponse)res.getSource();
//                response.setContentType("application/json;charset=UTF-8");
//                String resJson = SaResult.error(e.getMessage()).toString();
//                response.getWriter().print(resJson);
//                response.getWriter().flush();
//            } catch (IOException ex) {
//                throw new RuntimeException(ex);
//            }
//        };
//    }
}
